package swarm // Package swarm import "xemo.top/docker/docker/api/types/swarm"
import "time"

// Service represents a service
type Service struct {
	ID string
	Meta
	Spec         ServiceSpec   `json:",omitempty"`
	PreviousSpec *ServiceSpec  `json:",omitempty"`
	Endpoint     Endpoint      `json:",omitempty"`
	UpdateStatus *UpdateStatus `json:",omitempty"`
}

// UpdateStatus reports the status of a service update
type UpdateStatus struct {
	State UpdateState `json:",omitempty"`
}

// UpdateState 是服务更新的状态。
// UpdateState is the state of a service update.
type UpdateState string

const (
	// UpdateStateUpdating is the updating state.
	UpdateStateUpdating UpdateState = "updating"
	// UpdateStatePaused is the paused state.
	UpdateStatePaused UpdateState = "paused"
	// UpdateStateCompleted is the completed state.
	UpdateStateCompleted UpdateState = "completed"
	// UpdateStateRollbackStarted is the state with a rollback in progress
	UpdateStateRollbackStarted UpdateState = "rollback_started"
	// UpdateStateRollbackPaused is the state with a rollback in progress
	UpdateStateRollbackPaused UpdateState = "rollback_paused"
	// UpdateStateRollbackCompleted is the state with a rollback in progress
	UpdateStateRollbackCompleted UpdateState = "rollback_completed"
)

// ServiceSpec represents the spec of a service
type ServiceSpec struct {
	Annotations
	// TaskTemplate 定义在编排此服务时服务应如何构造新任务。
	// TaskTemplate defines how the service should construct new tasks when orchestrating this service.
	TaskTemplate   TaskSpec      `json:",omitempty"`
	Mode           ServiceMode   `json:",omitempty"`
	UpdateConfig   *UpdateConfig `json:",omitempty"`
	RollbackConfig *UpdateConfig `json:",omitempty"`
	// Networks ServiceSpec中的字段已弃用。应改用TaskSpec中的相同字段。此字段将在将来的版本中删除。
	// Networks field in ServiceSpec is deprecated.The same field in TaskSpec should be used instead.This field will be removed in a future release.
	Networks     []NetworkAttachmentConfig `json:",omitempty"`
	EndpointSpec *EndpointSpec             `json:",omitempty"`
}

// UpdateConfig represents the update configuration
type UpdateConfig struct {
	// Maximum 一次迭代中要更新的任务数0表示无限的并行性
	Parallelism uint64
	// Amount of time between updates
	Delay time.Duration `json:",omitempty"`
	// FailureAction 更新失败时要采取的操作。
	// FailureAction is the action to take when an update failures.
	FailureAction string `json:",omitempty"`
	// Monitor 指示在创建任务后监视任务失败的时间。如果任务在创建后以 “已拒绝” 、 “已完成” 或 “失败” 状态之一结束而失败，则将其计为失败。如果在监视后失败，它不算作失败。如果未指定Monitor，则将使用默认值。
	// Monitor indicates how long to monitor a task for failure after it is created.If the task fails by ending up in one of the states REJECTED,COMPLETED,or FAILED,within Monitor from its creation,this counts as a failure.If it fails after Monitor,it does not count as  a failure.If Monitor is unspecified,a default value will be used.
	Monitor time.Duration `json:",omitempty"`

	// MaxFailureRatio 在调用失败操作之前，更新期间可能失败的任务的分数。由当前更新创建的任何任务都以 “拒绝” 状态之一结束，从创建开始，在监控器内完成或失败都算作失败。失败数除以要更新的任务数，如果此分数大于MaxFailureRatio，则调用失败操作。
	// MaxFailureRatio is the fraction of tasks that may fail during an update before the failure action is invoked.Any task created by the current update which ends up in one of the states REJECTED,COMPLETED or FAILED within Monitor from its creation counts as a failure.The number of failures is divided by the number of tasks being updated,and if this fraction is greater than MaxFailureRatio,the failure action is invoked.
	//
	//如果故障动作是继续，则没有效果。如果失败操作为 “暂停”，则在启动另一次更新之前，不会更新更多的任务。
	//If the failure action is CONTINUE,there is no effect. If the failure action is PAUSE,no more tasks will be updated until another update is started.
	MaxFailureRatio float32
	// Order 指示在推出更新的任务时的操作顺序。在启动新任务之前关闭旧任务，或者在关闭旧任务之前启动新任务。
	// Order indicates the order of operations when rolling out an updated task.Either the old task is shut down before the new task is started,or the new task is started before the old task is shut down.
	Order string
}

// ServiceMode 表示服务的模式
// ServiceMode represents the mode of a service
type ServiceMode struct {
	Replicated *ReplicatedService `json:",omitempty"`
	Global     *GlobalService     `json:",omitempty"`
}

// GlobalService is a kind of ServiceMode.
type GlobalService struct{}

// ReplicatedService is a kind of ServiceMode
type ReplicatedService struct {
	Replicas *uint64 `json:",omitempty"`
}
